Zip for UWP
Zip エントリからメモリにファイルを抽出する
タスク別ヘルプ > Zip エントリからメモリにファイルを抽出する

zip からメモリ変数(バイト配列など)にファイルを抽出するには、次の関数を使用します。最初に、次の Imports(Visual Basic)/using(C#)文をコードの先頭に追加します。

Visual Basic コードの書き方

Visual Basic
コードのコピー
Imports C1.C1Zip and Imports System.IO

C# コードの書き方

C#
コードのコピー
using C1.C1Zip; and using System.IO

次のコードを追加します。

Visual Basic コードの書き方

Visual Basic
コードのコピー
Private Function GetDataFromZipFile(zipFileName As String, entryName As String) As Byte()
' zip ファイルからエントリを取得します。
Dim zip As New C1ZipFile()
zip.Open(zipFileName)
Dim ze As C1ZipEntry = zip.Entries(entryName)
' エントリデータをメモリストリームにコピーします。
Dim ms As New MemoryStream()
Dim buf(1000) As Byte
Dim s As Stream = ze.OpenReader()
Try
While True
Dim read As Integer = s.Read(buf, 0, buf.Length)
If read = 0 Then
Exit While
End If
ms.Write(buf, 0, read)
End While
Finally
s.Dispose()
End Try
s.Close()
' 結果を返します。
Return ms.ToArray()
End Function

C# コードの書き方

C#
コードのコピー
private byte[] GetDataFromZipFile(string zipFileName, string entryName)
{
// zip ファイルからエントリを取得します。
C1ZipFile zip = new C1ZipFile();
zip.Open(zipFileName);
C1ZipEntry ze = zip.Entries[entryName];
// エントリデータをメモリストリームにコピーします。
MemoryStream ms = new MemoryStream();
byte[] buf = new byte[1000];
using (Stream s = ze.OpenReader())
{
for (;;)
{
int read = s.Read(buf, 0, buf.Length);
if (read == 0) break;
ms.Write(buf, 0, read);
}
}
// C# には上の using ステートメントがあるため、close を呼び出す
// 必要はありませんが、VB では close が必要です。
//s.Close();
// 結果を返します。
return ms.ToArray();
}
関連トピック